﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RDPRemoteObject
{
    public class DB
    {
        static DataSet db=null;
        public static DataSet GetDB()
        {
            if (db == null)
            {
                db = new DataSet();
                ReadDB();
            }
            return db;
        }

        static string m_DBFile = AppDomain.CurrentDomain.BaseDirectory + "dsDB.xml";  
        static void ReadDB()
        {
            if (db != null)
            {
                db.Clear();
            }
            db.ReadXml(m_DBFile);
            db.AcceptChanges();
        }

        public static string GetConnStr(RDPCommon.Client.ClinetInfo ci)
        {
            DataRow[] drs = GetDB().Tables["dtDB"].Select("DBCaseCode='" + ci.Case + "' And DBCode='" + ci.DB + "'");
            if (drs.Length > 0)
            {
                string connStr = "" +
                    "server={0};database={1};user id={2};password={3};min pool size=4;max pool size=4;packet size=3072";
                return String.Format(
                    connStr,
                    drs[0]["DBServer"],
                    drs[0]["DBName"],
                    drs[0]["DBUser"],
                    RDPCommon.Util.DesDecrypt(drs[0]["DBPwd"].ToString())
                    );
            }
            return "";
        }

        public static int ExecuteNonQuery(
            RDPCommon.Client.ClinetInfo ci,
            CommandType cmdType, 
            string cmdText, 
            params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteNonQuery(
                GetConnStr(ci),
                cmdType,
                cmdText,
                commandParameters);            
        }

        public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteNonQuery(
                connection,
                cmdType,
                cmdText,
                commandParameters);  
        }

        public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteNonQuery(
                trans,
                cmdType,
                cmdText,
                commandParameters);  
        }

        public static SqlDataReader ExecuteReader(
            RDPCommon.Client.ClinetInfo ci,
            CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteReader(
                GetConnStr(ci),
                cmdType,
                cmdText,
                commandParameters
                );
        }

        public static object ExecuteScalar(RDPCommon.Client.ClinetInfo ci,
            CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteScalar(
                GetConnStr(ci),
                cmdType,
                cmdText,
                commandParameters
                );
        }

        public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            return SqlHelper.ExecuteScalar(
                connection,
                cmdType,
                cmdText,
                commandParameters
                );
        }

        public static DataSet ExecuteDataSet(RDPCommon.Client.ClinetInfo ci,
            CommandType cmdType, string cmdText, string dtName, params SqlParameter[] cmdParms)
        {
            return SqlHelper.ExecuteDataSet(
                GetConnStr(ci),
                cmdType,
                cmdText,
                dtName,
                cmdParms
                );
        }

        public static DataTable ExecuteDataTable(RDPCommon.Client.ClinetInfo ci,
            CommandType cmdType, string cmdText, string dtName, params SqlParameter[] cmdParms)
        {
            return SqlHelper.ExecuteDataTable(
                GetConnStr(ci),
                cmdType,
                cmdText,
                dtName,
                cmdParms
                );
        }
    }
}
